package com.tepth.remote.control.delegate.launcher;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tepth.remote.control.base.config.ApiDictionary;
import com.tepth.remote.control.base.convert.IStringDataConvert;
import com.tepth.remote.control.base.convert.ObjectConverter;
import com.tepth.remote.control.base.bean.Config;
import com.tepth.remote.core.net.callback.IError;
import com.tepth.remote.core.net.callback.ISuccess;
import com.tepth.remote.core.net.retrofit.RxRetrofitClient;

import io.reactivex.disposables.Disposable;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/8
 */
public class LauncherModel implements LauncherContract.Model {

    @Override
    public Disposable provideConfigData(String imei,
                                        final IStringDataConvert<Config> convert,
                                        IError error) {
        return RxRetrofitClient.builder()
                .url(ApiDictionary.API_GET_CONFIG)
                .params("Imei", imei)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(Object value) {
                        convert.convert(convertLoginBean(value));
                    }
                })
                .error(error)
                .builder()
                .get();
    }

    private Config convertLoginBean(Object response) {
        return new ObjectConverter<Config>().convert(JSON.toJSONString(response),
                new TypeReference<Config>() {
                });
    }
}
